package cn.mesmile.activity.controller;

import cn.hutool.core.date.DateUtil;
import cn.mesmile.activity.service.CommonService;
import lombok.AllArgsConstructor;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.print.attribute.HashAttributeSet;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zb
 * @date 2021/12/23 10:01
 * @Description
 */
@AllArgsConstructor
@RequestMapping("/api/v1/process")
@RestController
public class CommonServiceController {

    private final CommonService commonService;

    @GetMapping("/xml")
    public Map getXml(){
        String processDefinitionId = "vacationRequest:1:3";
        String xml = commonService.getXml(processDefinitionId);
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("xml", xml);
        return stringStringHashMap;
    }

    @GetMapping("/deploy")
    public Map deploy(){
        Deployment deployment = commonService.deployProcess();
        String id = deployment.getId();
        String name = deployment.getName();
        Date deploymentTime = deployment.getDeploymentTime();
        String category = deployment.getCategory();
        String key = deployment.getKey();
        Map<String, Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",name);
        map.put("deploymentTime", DateUtil.formatDateTime(deploymentTime));
        map.put("category",category);
        map.put("key",key);
        return map;
    }

    @GetMapping("/startProcess")
    public Map startProcess(){
        ProcessInstance processInstance = commonService.startProcess();
        Map<String, Object> variables = processInstance.getProcessVariables();
        String id = processInstance.getId();
        String businessKey = processInstance.getBusinessKey();
        String processDefinitionId = processInstance.getProcessDefinitionId();
        String processDefinitionName = processInstance.getProcessDefinitionName();
        Date startTime = processInstance.getStartTime();
        String startUserId = processInstance.getStartUserId();
        String activityId = processInstance.getActivityId();

        Map<String, Object> map = new HashMap<>();
        map.put("variables",variables);
        map.put("id",id);
        map.put("startTime", DateUtil.formatDateTime(startTime));
        map.put("businessKey",businessKey);
        map.put("startUserId",startUserId);
        map.put("processDefinitionId",processDefinitionId);
        map.put("processDefinitionName",processDefinitionName);
        map.put("activityId",activityId);

        return map;
    }

    @GetMapping("/queryTask")
    public Map queryTask(){
        commonService.queryTask();
        return new HashMap();
    }


    @GetMapping("/completeTask")
    public Map completeTask(){
        commonService.completeTask();
        return new HashMap();
    }

    @GetMapping("/stop")
    public Map stop(@RequestParam("processInstanceId")String processInstanceId){
        commonService.stopProcess(processInstanceId);
        HashMap map = new HashMap();
        map.put("data","");
        map.put("message","停止流程成功");
        map.put("code",200);
        return map;
    }

    @GetMapping("/getComment")
    public Map getComment(@RequestParam("taskId")String taskId){
        commonService.findComment(taskId);
        HashMap map = new HashMap();
        map.put("data","");
        map.put("message","停止流程成功");
        map.put("code",200);
        return map;
    }


}
